<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:46
 */
class Base extends MY_Admin_Controller
{
    public function index()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	
    	$limit = 20;
    	$offset = ($page-1) * $limit;
    	
        $this->load->model('article/model_article','article');
        $total_rows = $this->article->count();
        $articleInfo = $this->article->lists($offset, $limit);

        $this->load->model('article/model_article_cate','cate');
        $cateInfo = $this->cate->getAllLists();

        //分页
    	$this->load->library('pagination');
    	
    	$config['total_rows'] = $total_rows;
    	$config['per_page'] = $limit;
    	$config['base_url'] = '/article/Base/index';
    	
    	$this->pagination->initialize($config);
    	$show_page = $this->pagination->create_links();

        $data = array(
            'articleInfo' => $articleInfo,
            'cateInfo'    => $cateInfo,
        	'show_page'   => $show_page,
        	'total_rows'  => $config['total_rows'],
        );

        $this->load->view('admin/article/base/index',$data);
    }


    public function create()
    {
        $aid = $this->uri->segment(4);

        $this->load->model('article/model_article','article');
        $articleInfo = $this->article->getById($aid);

        $this->load->model('article/model_article_cate','cate');
        $cateInfo = $this->cate->getAllLists();

        $this->load->model('common/Model_file','file');
        $fileInfo = $this->file->getById($articleInfo['pic']);

        $data = array(
            'articleInfo'  => $articleInfo,
            'cateInfo'     => sort_data($cateInfo),
            'fileInfo'     => $fileInfo
        );

        $this->load->view('admin/article/base/create',$data);
    }

    /*
    * 竞拍资料编辑后保存
    *
    * */
    public function save()
    {
        $aid = (int)$this->input->get_post('aid');


        //图片未存入
        $data = array(
            'cid'           =>     (int)$this->input->get_post('cid'),
            'name'          =>     trim($this->input->get_post('name')),
        	'create_time'   =>     trim($this->input->get_post('create_time')),
            'content'       =>     $this->input->get_post('content'),
            'title'         =>     trim($this->input->get_post('title')),
            'keyword'       =>     trim($this->input->get_post('keyword')),
            'descr'         =>     trim($this->input->get_post('descr')),
            'is_view'       =>     (int)$this->input->get_post('is_view'),
            'is_top'        =>     (int)$this->input->get_post('is_top'),
            'sort'          =>     (int)$this->input->get_post('sort'),
            'is_valid'      =>     (int)$this->input->get_post('is_valid'),
            'is_invalid'    =>     (int)$this->input->get_post('is_invalid'),
            'source'        =>     $this->input->get_post('source'),

            //判断是否选择图片如果选择了图片则获取新突破地址或者判断是新建页面还是修改页面
            'pic'           =>     !empty($_FILES['up_file']['tmp_name'])?$this->uploadFile('up_file'):(isset($aid)?$this->input->get_post('pic'):''),
        );


        //更新
        $this->load->model('article/model_article','article');
        $save_rs = $this->article->save($data, $aid ? $aid : null);
        if (!$save_rs)
        {
            show_error('添加资讯文章失败');
        }


        $this->load->helper('url');
        redirect(url('admin').'article/base/index');
    }


    /**
     * 删除道具
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $aid = (int)$this->input->get_post('aid');

            if (!$aid)
            {
                $response = error(30001);
                break;
            }

            $this->load->model('article/model_article','article');
            $res = $this->article->delete(array('aid' => $aid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    /*
     *
     * 文章分类
     * */
    public function cate_index()
    {
        $this->load->model('article/model_article_cate','cate');
        $cateInfo = $this->cate->getAllLists();

        $data = array(
            'cateInfo'  =>   sort_data($cateInfo),
        );

        $this->load->view('admin/article/base/cate_index',$data);
    }

    public function cate_create()
    {
        $cid = $this->uri->segment(4);

        $this->load->model('article/model_article_cate','cate');
        $cateInfo = $this->cate->getById($cid);
        $cateAll = $this->cate->getAllLists();

        if($cid){
            //找出当前下面的分类
            $sonInfo = get_son($cateAll,$cid);
            //取出所有分类下包含的下级分类
            foreach($sonInfo as $k=>$v){
                unset($cateAll[$k]);
            }
        }

        $data = array(
            'cateInfo'     => $cateInfo,
            'cateAll'      => sort_data($cateAll),
        );

        $this->load->view('admin/article/base/cate_create',$data);
    }

    public function cate_save()
    {
        $cid = (int)$this->input->get_post('cid');
        $this->load->helper('pinyin');

        //图片未存入
        $data = array(
            'name'          =>     trim($this->input->get_post('name')),
            'pid'           =>     (int)$this->input->get_post('pid'),
            'sort'          =>     (int)$this->input->get_post('sort'),
            'path'          =>     pinyin(trim($this->input->get_post('path'))),
            'title'         =>     trim($this->input->get_post('title')),
            'keyword'       =>     trim($this->input->get_post('keyword')),
            'descr'         =>     trim($this->input->get_post('descr')),
        );

        //更新
        $this->load->model('article/model_article_cate','cate');
        $this->cate->save($data, $cid ? $cid : null);

        $this->load->helper('url');
        redirect(url('admin').'article/base/cate_index');
    }

    /**
     * 删除分类
     */
    public function cate_delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $cid = (int)$this->input->get_post('cid');

            if (!$cid)
            {
                $response = error(30010);
                break;
            }

            $this->load->model('article/model_article','article');
            $item = $this->article->getOne(array('cid' => $cid));

            if (!empty ($item))
            {
                $response = error(30012);
                break;
            }

            $this->load->model('article/model_article_cate', 'article_cate');

            $item = $this->article_cate->getOne(array('pid' => $cid));
            if (!empty ($item))
            {
                $response = error(30013);
                break;
            }

            $item = $this->article_cate->delete(array('cid' => $cid));

            if (empty ($item))
            {
                $response = error(10080);
                break;
            }


        }while (false);


        $this->json_output($response);
    }


}